#!/bin/sh
################################################################################
# shutdown
################################################################################
. /etc/rc.d/functions

start() {

    local script
    local mountpoint
    local mountpoints
    local retry

    mm_message_output info "finishing system shutdown ..."

    # mountpoint awk script.
    # finds mountpoints in /proc/mounts that are to be unmounted.
    script='$2 ~ /^(\/|\/initrd)$/ {next}
            $2 ~ /^\/media\//      {print $2 ; next}
            $2 ~ /^\/\/minimyth$/  {print $2 ; next}
            $3 ~ /^cifs$/          {print $2 ; next}
            $3 ~ /^nfs$/           {print $2 ; next}'

    # unmount any remaining file systems.
    mountpoints=`LANG=C /usr/bin/awk "${script}" '/proc/mounts' | /usr/bin/sort -r`
    retry=3
    while [ -n "${mountpoints}" ] && [ ${retry} -gt 0 ] ; do
        for mountpoint in ${mountpoints} ; do
            mm_message_output info "unmounting ${mountpoint}"
            /bin/umount -f ${mountpoint}
        done
        mountpoints=`LANG=C /usr/bin/awk "${script}" '/proc/mounts' | /usr/bin/sort -r`
        retry=$(( ${retry} - 1 ))
        if [ -n "${mountpoints}" ] && [ ${retry} -gt 0 ] ; then
            sleep 1
        fi
    done

    return 0
}

stop() {
    start
    return 0
}

case $1 in
    start) start ;;
    stop)  stop  ;;
esac

exit 0
